﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AESUtil
{
    /// <summary>
    /// Setting.xaml 的交互逻辑
    /// </summary>
    public partial class Setting : Window
    {
        public const String KEY_SM4 = "Giw7qU0ZsaNDrtKeDnxYmA==";
        public const String IV_SM4 = "azBqlF0L3trfd/mnuxMbYg==";
        public const String KEY_AES = "624eff943f0c477d8328bb7cd71d0445";
        public const String IV_AES = "4ae9bc3e771db942";

        public Setting()
        {
            InitializeComponent();
            this.formatCheckBox.IsChecked = Properties.Settings.Default.autoFormat;
            this.keyTextBox.Text = Properties.Settings.Default.KEY;
            this.ivTextBox.Text = Properties.Settings.Default.IV;
            this.showRowNumCheckBox.IsChecked = Properties.Settings.Default.showRowNum;
            this.historyDialogCheckBox.IsChecked = Properties.Settings.Default.showHistoryDialog;
            if (Properties.Settings.Default.algorithm.Equals("SM4"))
            {
                this.sm4RadioBtn.IsChecked = true;
            }else
            {
                this.aesRadioBtn.IsChecked = true;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.autoFormat = (bool)this.formatCheckBox.IsChecked;
            Properties.Settings.Default.showHistoryDialog = (bool)this.historyDialogCheckBox.IsChecked;
            Properties.Settings.Default.KEY = this.keyTextBox.Text;
            Properties.Settings.Default.IV = this.ivTextBox.Text;
            Properties.Settings.Default.showRowNum = (bool)this.showRowNumCheckBox.IsChecked;
            string algorithm;
            if (this.sm4RadioBtn.IsChecked == true)
            {
                algorithm = "SM4";
                
            }
            else
            {
                algorithm = "AES";
            }
            Properties.Settings.Default.algorithm = algorithm;
            (this.Owner as Window).Title = "加解密程序  算法：" + algorithm;
            Properties.Settings.Default.Save();
            this.Close();
        }

        private void resetKeyBtn_Click(object sender, RoutedEventArgs e)
        {
            if(this.sm4RadioBtn.IsChecked == true)
            {
                this.keyTextBox.Text = KEY_SM4;
            }
            else
            {
                this.keyTextBox.Text = KEY_AES;
            }
        }

        private void resetIVBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.sm4RadioBtn.IsChecked == true)
            {
                this.ivTextBox.Text = IV_SM4;
            }
            else
            {
                this.ivTextBox.Text = IV_AES;
            }
        }

        private void sm4RadioBtn_Checked(object sender, RoutedEventArgs e)
        {
            this.keyTextBox.Text = KEY_SM4;
            this.ivTextBox.Text = IV_SM4;
        }

        private void aesRadioBtn_Checked(object sender, RoutedEventArgs e)
        {
            this.keyTextBox.Text = KEY_AES;
            this.ivTextBox.Text = IV_AES;
        }
    }
}
